# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from Netease.items import UserItem, RecordItem, SongItem
from Netease.mysqlpiplines.sql import Sql


class UserPipeline(object):
    def process_item(self, item, spider):
        if isinstance(item, UserItem):
            userId = item['userId']
            nickname = item['nickname']
            avatarUrl = item['avatarUrl']
            gender = item['gender']
            signature = item['signature']
            follows = item['follows']
            followeds = item['followeds']
            eventCount = item['eventCount']
            playlistCount = item['playlistCount']
            level = item['level']
            totalCount = item['totalCount']
            userType = item['userType']
            area = item['area']
            Sql.insert_or_update_user(userId,nickname,avatarUrl,gender,signature,follows,followeds,eventCount,playlistCount,level,totalCount,userType,area)
        return item

class RecordPipeline(object):
    def process_item(self, item, spider):
        if isinstance(item, RecordItem):
            userId = item['userId']
            top = item['top']
            Sql.insert_or_update_record(userId,top)
        return item

class SongPipeline(object):
    def process_item(self, item, spider):
        if isinstance(item, SongItem):
            songId = item['songId']
            songname = item['songname']
            artists = item['artists']
            albumId = item['albumId']
            albumname = item['albumname']
            comments = item['comments']
            mvId = item['mvId']
            Sql.insert_or_update_song(songId,songname,artists,albumId,albumname,comments,mvId)
        return item